﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.Workspace
{
    public partial class ContractProfitPut : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ContractID"] != null)
                {
                    //销售收入
                    decimal TotalPrice = 0;
                    ContractBll bll = new ContractBll();
                    TotalPrice = bll.SumContractItemPrice(new Guid(Request.QueryString["ContractID"]));

                    lblContractTotalPrice.Text = TotalPrice.ToString();

                    //成本
                    decimal TotalCost = 0;
                    decimal MachineCost = 0, ReagentCost = 0;
                    decimal PresentCost = 0, CapitalCost = 0, RepairCost = 0;

                    MachineCost = bll.SumContractItemCost(new Guid(Request.QueryString["ContractID"]), 2);
                    ReagentCost = bll.SumContractItemCostFor01(new Guid(Request.QueryString["ContractID"]));

                    MachineCost = decimal.Round(MachineCost, 2);
                    ReagentCost = decimal.Round(ReagentCost, 2);

                    lblMachineCost.Text = MachineCost.ToString();
                    lblReagentCost.Text = ReagentCost.ToString();

                    CtrtContractEntity entity = bll.Get<CtrtContractEntity>(new Guid(Request.QueryString["ContractID"]));
                    CapitalCost = entity.CapitalCost;
                    PresentCost = entity.GivenMaterialCost;
                    RepairCost = entity.RepairCost;

                    txtCapitalCost.Text = CapitalCost.ToString();
                    txtPresentCost.Text = PresentCost.ToString();
                    txtRepairCost.Text = RepairCost.ToString();

                    TotalCost = MachineCost + ReagentCost + PresentCost + CapitalCost + RepairCost;

                    lblContractTotalCost.Text = TotalCost.ToString();

                    //计算利润
                    lblProfit.Text = (TotalPrice - TotalCost).ToString();

                    if (TotalPrice != 0)
                        lblProfitRate.Text = ((TotalPrice - TotalCost) / TotalPrice).ToString("0.00");
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            decimal GivenMaterialCost = 0, CapitalCost = 0, RepairCost = 0;

            decimal.TryParse(txtPresentCost.Text.Trim(), out GivenMaterialCost);
            decimal.TryParse(txtCapitalCost.Text.Trim(), out CapitalCost);
            decimal.TryParse(txtRepairCost.Text.Trim(), out RepairCost);

            ContractBll bll = new ContractBll();
            CtrtContractEntity entity = bll.Get<CtrtContractEntity>(new Guid(Request.QueryString["ContractID"]));
            entity.GivenMaterialCost = GivenMaterialCost;
            entity.CapitalCost = CapitalCost;
            entity.RepairCost = RepairCost;

            bll.Update(entity);

            decimal Cost = 0, Price = 0;
            Cost = Convert.ToDecimal(lblContractTotalPrice.Text);
            Price = Convert.ToDecimal(lblContractTotalCost.Text);

            lblProfit.Text = (Price - Cost - GivenMaterialCost - CapitalCost - RepairCost).ToString();

            if (Price != 0)
                lblProfitRate.Text = ((Price - Cost - GivenMaterialCost - CapitalCost - RepairCost) / Price).ToString();
        }
    }
}
